package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.dto.OrdersDto;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequestMapping("/order")
@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    //用户下单
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("下单数据：{}",orders);
        orderService.submit(orders);

        return R.success("下单成功");
    }

    //用户订单详情
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number){
        //分页构造器
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.like(number !=null,Orders::getNumber,number);
        //添加排序条件，根据时间降序排序
        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,ordersDtoPage,"records");
        List<Orders> records = pageInfo.getRecords();

        List<OrdersDto> list =records.stream().map((item) ->{
            OrdersDto ordersDto =new OrdersDto();
            //对象拷贝
            BeanUtils.copyProperties(item,ordersDto);
            //订单ID
            Long Id = item.getId();

            //根据Id查询订单对象
            Orders orders = orderService.getById(Id);
            if (orders != null) {
                String userName = orders.getConsignee();
                String phone = orders.getPhone();
                String address = orders.getAddress();
                String consignee = orders.getConsignee();
            }
            return ordersDto;

        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(list);
        return R.success(ordersDtoPage);
    }

    //用户端订单详情
    //用户订单详情
    @GetMapping("/userPage")
    public R<Page> phonePage(int page,int pageSize){
        //分页构造器
        Page<Orders> order = new Page<>(page,pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据时间降序排序
        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(order,queryWrapper);


        return R.success(order);
    }

    //修改订单状态
    @PutMapping
    public R<String> setStatus(@RequestBody Orders orders){
        log.info("Order订单状态：{}",orders);

        orders.setStatus(3);

        orderService.updateById(orders);

        return R.success("订单状态修改成功！");

    }


}
